﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;

namespace YJKStairs.CommonUnit
{
	public class ComponentTypeManager
	{
		public ComponentTypeManager(Autodesk.Revit.DB.Document doc)
		{
			this.m_Doc = doc;
			this.InitializeStairsType();
			this.InitializeRailingType();
			this.InitializeTopRailType();
			this.InitializeSupportType();
			this.InitializeUsingType();
		}

		public static int GetYJKStairsTypeNumber(Autodesk.Revit.DB.Document doc)
		{
			List<int> list = new List<int>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(StairsType)))
			{
				StairsType stairsType = element as StairsType;
				if (stairsType != null)
				{
					string name = stairsType.Name;
					if (StairsConstNameManager.IsStartsWithStairsName(name))
					{
						int num = name.IndexOf("-");
						if (num != -1)
						{
							string value = name.Substring(num + 1);
							try
							{
								int item = Convert.ToInt32(value);
								list.Add(item);
							}
							catch (Exception)
							{
							}
						}
					}
				}
			}
			list.Sort();
			if (list.Count == 0)
			{
				return 1;
			}
			return list[list.Count - 1] + 1;
		}

		public static CutMarkType GetCutMarkTypeByName(Autodesk.Revit.DB.Document doc, CutMarkType curType, string sNewName)
		{
			CutMarkType cutMarkType = null;
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(CutMarkType)))
			{
				CutMarkType cutMarkType2 = element as CutMarkType;
				if (cutMarkType2 != null && cutMarkType2.Name.Equals(sNewName))
				{
					cutMarkType = cutMarkType2;
					break;
				}
			}
			if (cutMarkType == null)
			{
				CutMarkType cutMarkType3 = curType.Duplicate(sNewName) as CutMarkType;
				if (cutMarkType3 != null)
				{
					cutMarkType3.get_Parameter(BuiltInParameter.CUT_LINE_ANGLE).Set(0.0);
					cutMarkType3.get_Parameter(BuiltInParameter.CUT_MARK_SYMBOL).Set(0);
					cutMarkType = cutMarkType3;
				}
			}
			return cutMarkType;
		}

		public static bool GetStairsRunType(Autodesk.Revit.DB.Document doc, string name, ref StairsRunType runType)
		{
			new List<int>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(StairsRunType)))
			{
				StairsRunType stairsRunType = element as StairsRunType;
				if (stairsRunType != null && stairsRunType.Name.CompareTo(name) == 0)
				{
					runType = stairsRunType;
					return true;
				}
			}
			return false;
		}

		public static bool GetStarisLandingType(Autodesk.Revit.DB.Document doc, string name, ref StairsLandingType landingType)
		{
			new List<int>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(StairsLandingType)))
			{
				StairsLandingType stairsLandingType = element as StairsLandingType;
				if (stairsLandingType != null && stairsLandingType.Name.CompareTo(name) == 0)
				{
					landingType = stairsLandingType;
					return true;
				}
			}
			return false;
		}

		public static bool GetRailingType(Autodesk.Revit.DB.Document doc, string name, ref RailingType railingType)
		{
			new List<int>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(RailingType)))
			{
				RailingType railingType2 = element as RailingType;
				if (railingType2 != null && railingType2.Name.CompareTo(name) == 0)
				{
					railingType = railingType2;
					return true;
				}
			}
			return false;
		}

		public List<string> GetStairsTypeNameList()
		{
			List<string> list = new List<string>();
			foreach (StairsType stairsType in this.m_StairsType)
			{
				list.Add(stairsType.Name);
			}
			return list;
		}

		public List<string> GetRailingTypeNameList()
		{
			List<string> list = new List<string>();
			foreach (RailingType railingType in this.m_RailingType)
			{
				list.Add(railingType.Name);
			}
			return list;
		}

		public List<string> GetHeightsOfRailingTypeList()
		{
			List<string> list = new List<string>();
			foreach (RailingType railingType in this.m_RailingType)
			{
				list.Add(Common.FeetToMM(railingType.TopRailHeight).ToString("f0"));
			}
			return list;
		}

		public List<string> GetTopRailTypeNamesOfRailingTypeList()
		{
			List<string> list = new List<string>();
			foreach (RailingType railingType in this.m_RailingType)
			{
				try
				{
					Element element = this.m_Doc.GetElement(railingType.TopRailType);
					list.Add(element.Name);
				}
				catch (Exception)
				{
					list.Add("");
				}
			}
			return list;
		}

		public List<string> GetTopRailTypeNameList()
		{
			List<string> list = new List<string>();
			foreach (TopRailType topRailType in this.m_TopRailType)
			{
				list.Add(topRailType.Name);
			}
			return list;
		}

		public List<string> GetSupportTypeNameList()
		{
			List<string> list = new List<string>();
			foreach (ElementType elementType in this.m_SupportType)
			{
				list.Add(elementType.Name);
			}
			return list;
		}

		public List<string> GetUsingRangeList()
		{
			return this.m_StairsUsingRange;
		}

		public StairsType GetStairsType(int index)
		{
			if (index < 0 || index >= this.m_StairsType.Count)
			{
				return null;
			}
			return this.m_StairsType[index];
		}

		private StairsType GetStairsTypeByName(string strName)
		{
			StairsType result = null;
			foreach (StairsType stairsType in this.m_StairsType)
			{
				if (stairsType.Name.Contains(strName))
				{
					result = stairsType;
					break;
				}
			}
			return result;
		}

		private StairsType GetDefaultStairsType()
		{
			StairsType result = null;
			foreach (StairsType stairsType in this.m_StairsType)
			{
				if (stairsType.FamilyName.Equals(ComponentTypeManager.m_strStairsTypeFamilyName))
				{
					result = stairsType;
					break;
				}
			}
			return result;
		}

		public StairsType GetStairsType()
		{
			StairsType stairsType = this.GetStairsTypeByName(ComponentTypeManager.m_strStairsTypeName);
			if (stairsType == null)
			{
				stairsType = this.GetDefaultStairsType();
			}
			return stairsType;
		}

		public RailingType GetRailingType(int index)
		{
			if (index < 0 || index >= this.m_RailingType.Count)
			{
				return null;
			}
			return this.m_RailingType[index];
		}

		public TopRailType GetTopRailType(int index)
		{
			if (index < 0 || index >= this.m_TopRailType.Count)
			{
				return null;
			}
			return this.m_TopRailType[index];
		}

		public ElementType GetSupportType(int index)
		{
			if (index < 0 || index >= this.m_SupportType.Count)
			{
				return null;
			}
			return this.m_SupportType[index];
		}

		private void InitializeStairsType()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(StairsType)))
			{
				StairsType stairsType = element as StairsType;
				if (stairsType != null && !StairsConstNameManager.IsStartsWithStairsName(stairsType.Name))
				{
					this.m_StairsType.Add(stairsType);
				}
			}
		}

		private void InitializeRailingType()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(RailingType)))
			{
				RailingType railingType = element as RailingType;
				if (railingType != null)
				{
					this.m_RailingType.Add(railingType);
				}
			}
		}

		private void InitializeTopRailType()
		{
			new List<TopRailType>();
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(TopRailType)))
			{
				TopRailType topRailType = element as TopRailType;
				if (topRailType != null)
				{
					this.m_TopRailType.Add(topRailType);
				}
			}
		}

		private void InitializeSupportType()
		{
			new List<ElementType>();
			foreach (Element element in new FilteredElementCollector(this.m_Doc).OfClass(typeof(ElementType)).OfCategory(BuiltInCategory.OST_StairsStringerCarriage))
			{
				ElementType elementType = element as ElementType;
				if (elementType != null)
				{
					this.m_SupportType.Add(elementType);
				}
			}
		}

		private void InitializeUsingType()
		{
			this.m_StairsUsingRange.Add("住宅共用楼梯（有电梯）");
			this.m_StairsUsingRange.Add("住宅共用楼梯（无电梯）");
			this.m_StairsUsingRange.Add("住宅套内楼梯");
			this.m_StairsUsingRange.Add("宿舍");
			this.m_StairsUsingRange.Add("老年居住建筑");
			this.m_StairsUsingRange.Add("老年公共建筑");
			this.m_StairsUsingRange.Add("幼儿园");
			this.m_StairsUsingRange.Add("小学");
			this.m_StairsUsingRange.Add("电影院");
			this.m_StairsUsingRange.Add("剧场");
			this.m_StairsUsingRange.Add("体育馆");
			this.m_StairsUsingRange.Add("商场");
			this.m_StairsUsingRange.Add("医院");
			this.m_StairsUsingRange.Add("旅馆");
			this.m_StairsUsingRange.Add("展览馆");
			this.m_StairsUsingRange.Add("疗养院");
			this.m_StairsUsingRange.Add("大学");
			this.m_StairsUsingRange.Add("中学");
			this.m_StairsUsingRange.Add("办公");
			this.m_StairsUsingRange.Add("其他建筑");
			this.m_StairsUsingRange.Add("专用疏散楼梯");
			this.m_StairsUsingRange.Add("服务楼梯");
		}

		public static string m_strStairsTypeName = "整体浇筑楼梯";

		public static string m_strStairsTypeFamilyName = "现场浇注楼梯";

		private Document m_Doc;

		private List<StairsType> m_StairsType = new List<StairsType>();

		private List<RailingType> m_RailingType = new List<RailingType>();

		private List<TopRailType> m_TopRailType = new List<TopRailType>();

		private List<ElementType> m_SupportType = new List<ElementType>();

		private List<string> m_StairsUsingRange = new List<string>();
	}
}
